<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div class="btn">
        <button data-path="/home">首页</button>
        <button data-path="/login">登陆</button>
        <button data-path="/news">新闻</button>
    </div>
    <div id="com"></div>
</body>
<script>
    const oCom = document.getElementById("com");
    const oBtn = document.querySelectorAll(".btn button");

    //路由表
    const routes = [
        {path:"/home",component:"这是首页"},
        {path:"/login",component:"这是登陆页"},
        {path:"/news",component:"这是新闻页"},
    ]
    oBtn.forEach(item=>{
        item.onclick = function (){
            location.hash = this.dataset.path
        }
    })

    window.onhashchange = function (e){
        const newUrl = e.newURL.split("#")[1];
        routes.forEach(item=>{
            if(item.path === newUrl){
                oCom.innerHTML = item.component
            }
        })
    }
</script>
</html>